import Vue from 'vue'
import Vuex from 'vuex'
import store from '../store'
let vue = Vue.use(Vuex)
export default class sysConfigUtil {

    constructor(options) {
    }

    getColumns() {
        return [
          { prop: 'value', label: '值', sortable: true, width: '100', formatter: this.stringFormater },
          { prop: 'description', label: '描述', sortable: true, width: '500', formatter: this.stringFormater }
        ]
    }

    listCategory(sucCallback, failedCallback){
        vue.http.post('/hr/system/field/list').then(response => {
            console.log("list category response" + response);
            if (response.body.result == 1) {
                console.log('list category ' + JSON.stringify(response.body));
                sucCallback(response)
              }
              else{
                failedCallback(response)
              }
        }, response => {
            if(failedCallback)
                failedCallback(response)
        })
    }

    parseData(configs){
        // for(var index in configs){
        //     store.commit('SET_SYSCONFIG',configs[index].name,configs[index].configs)
        // }
        
        store.commit('SET_SYSCONFIG',configs)
    }

    listDeparts(sucCallback, failedCallback){
        var that = this
        vue.http.post('/hr/department/list')
        .then(response => {
            if (response.body.result == 1) {
                console.log('listAll response ' + JSON.stringify(response.body));
                that.parseData(response.body.body)
                if(sucCallback)
                    sucCallback(response)
              }
              else{
                  if(failedCallback)
                    failedCallback(response)
              }
        }, response => {
            if(failedCallback)
                failedCallback(response)
        })
    }

    listAll(sucCallback, failedCallback){
        var that = this
        vue.http.post('/hr/system/field/listAll').then(response => {
            console.log("listAll response" + response);
            if (response.body.result == 1) {
                console.log('listAll response ' + JSON.stringify(response.body));
                that.parseData(response.body.body)
                if(sucCallback)
                    sucCallback(response)
              }
              else{
                  if(failedCallback)
                    failedCallback(response)
              }
        }, response => {
            if(failedCallback)
                failedCallback(response)
        })
    }

    updateConfig(configs,sucCallback, failedCallback){
        vue.http.post('/hr/system/field/update',JSON.stringify(configs)).then(response => {
            console.log("update response" + response);
            if (response.body.result == 1) {
                console.log('update response ' + JSON.stringify(response.body));
                sucCallback(response)
              }
              else{
                failedCallback(response)
              }
        }, response => {
            if(failedCallback)
                failedCallback(response)
        })
    }

    getConfig(field,sucCallback, failedCallback){
        vue.http.get('/hr/system/field/get?field='+field).then(response => {
            console.log("get response" + response);
            if (response.body.result == 1) {
                console.log('get response ' + JSON.stringify(response.body));
                sucCallback(response)
              }
              else{
                failedCallback(response)
              }
        }, response => {
            if(failedCallback)
                failedCallback(response)
        })
    }

}